package com.cxh.service.impl;

import com.cxh.base.Data2ServiceImpl;
import com.cxh.util.DataSource;
import com.cxh.dao.CarCategoryDao;
import com.cxh.dao.CarSeriesDao;
import com.cxh.dao.CarTypeDao;
import com.cxh.entity.CarCategory;
import com.cxh.entity.CarSeries;
import com.cxh.entity.CarType;
import com.cxh.service.CarSeriesService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 类说明：学生表 
 * @author  YangHL
 * @date  2018-10-15 09:53:21
 */
@Service("carSeriesService")
@DataSource("dataSource2")
public class CarSeriesServiceImpl extends Data2ServiceImpl<CarSeries> implements CarSeriesService {
    @Autowired
    private CarSeriesDao carSeriesDao;
    @Autowired
    private CarTypeDao carTypeDao;
    @Autowired
    private CarCategoryDao carCategoryDao;


    @Override
    public List<CarSeries> loadPageList(Map<String, Object> map) {
       return carSeriesDao.loadPageList(map);
    }
    
    @Override
    public void insertImportData(String josnInfo) {

		//解析报文
		JSONObject jsonObject = JSONObject.fromObject(josnInfo);
		//获取result字段信息
		Object resultOBObject = jsonObject.get("result");
		//转为json数组
		JSONArray resultArray = JSONArray.fromObject(resultOBObject);
		for (Object result : resultArray) {
			JSONObject resultJson = JSONObject.fromObject(result);
			//获取品牌id
			String seriesId = resultJson.get("I").toString();
			//获取品牌名称
			String seriesName = resultJson.get("N").toString();
			//插入车系品牌表
			CarSeries carSeries = new CarSeries();
			carSeries.setSeriesid(seriesId);
			carSeries.setSeriesname(seriesName);
			carSeriesDao.insert(carSeries);
			
			//车系类型
			Object typeObject = resultJson.get("List");
			JSONArray typeArray = JSONArray.fromObject(typeObject);
			for (Object type : typeArray) {
				JSONObject typeJson = JSONObject.fromObject(type);
				//类型id
				String typeId = typeJson.get("I").toString();
				//类型名称
				String typeName = typeJson.get("N").toString();
				CarType carType = new CarType();
				carType.setSeriesid(seriesId);
				carType.setTypeid(typeId);
				carType.setTypename(typeName);
				//插入车类型表
				carTypeDao.insert(carType);
				
				//车系类别
				Object categoryObject = typeJson.get("List");
				JSONArray categoryArray = JSONArray.fromObject(categoryObject);
				for (Object category : categoryArray) {
					JSONObject categoryJson = JSONObject.fromObject(category);
					String categoryId = categoryJson.get("I").toString();
					String categoryName = categoryJson.get("N").toString();
					CarCategory carCategory = new CarCategory();
					carCategory.setTypeid(typeId);
					carCategory.setCarcategoryid(categoryId);
					carCategory.setCarcategoryname(categoryName);
					//插入车系类别表
					carCategoryDao.insert(carCategory);
				}
			}
		}
    }
    
    @Override
    public List<CarSeries> getCarSeriesBySeriesId(String seriesid) {
    	return carSeriesDao.getCarSeriesBySeriesId(seriesid);
    }
}
